//
//  HomeSearchView.m
//  DuBai
//
//  Created by darktm on 14-3-1.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "HomeSearchView.h"

@implementation HomeSearchView

@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        UIImageView *bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 53)];
        bg.image = [UIImage imageNamed:@"home_img_search_bg.png"];
        bg.userInteractionEnabled = YES;
        [self addSubview:bg];
        
        
        UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(5, 11, 250, 31)];
//        search.backgroundImage = [Utils imageWithColor:[UIColor clearColor] size:CGSizeMake(250, 31)];
        search.barStyle = UIBarStyleBlack;
        search.delegate = self;
        [self addSubview:search];
        self.sb = search;
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(270, 5, 44, 44)];
        [btn setTitle:@"取消" forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        [btn addTarget:self action:@selector(on_btn_cancle:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        
        self.cancle = btn;
        
    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self.sb resignFirstResponder];
    
    if(delegate && [delegate respondsToSelector:@selector(didCancle:)])
        [delegate didCancle:self];
}

-(void)willMoveToSuperview:(UIView *)newSuperview{
//    DLog(@"");
    self.sb.delegate = self;

    [self.sb becomeFirstResponder];
}

-(void)willRemoveSubview:(UIView *)subview{
//    DLog(@"");
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
    [self.sb resignFirstResponder];//删除当前页面
    
    if(delegate && [delegate respondsToSelector:@selector(didSearch:)])
        [delegate didSearch:self];
}

- (void)on_btn_cancle:(id)sender {
    
    [self.sb resignFirstResponder];//删除当前页面
    
    if(delegate && [delegate respondsToSelector:@selector(didCancle:)])
        [delegate didCancle:self];    
}
@end
